//
//  HomeTableViewCell.m
//  AppointmentForCar
//
//  Created by TheMacBook on 2016/11/7.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "HomeTableViewCell.h"
#import "HomeHistoryModel.h"

@implementation HomeTableViewCell {
    HomeTableViewDate * _date;
    HomeTableViewItem * _from;
    HomeTableViewItem * _to;
    UILabel * _statusLabel;
    UILabel * _nameLabel;
    UILabel * _phone;
    UIView * _line;
    UIView * _bottomLine;
    UIImageView *_avatarImageView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        _date = [[HomeTableViewDate alloc] init];
        _from = [[HomeTableViewItem alloc] init];
        _to = [[HomeTableViewItem alloc] init];
        _nameLabel = [[UILabel alloc] init];
        _statusLabel = [[UILabel alloc] init];
        _phone = [[UILabel alloc] init];
        _line = [[UIView alloc] init];
        _phone.textColor = [UIColor whiteColor];
        _nameLabel.textColor = [UIColor whiteColor];
        _statusLabel.textColor = [UIColor whiteColor];
        _statusLabel.textAlignment = NSTextAlignmentRight;
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _avatarImageView = [[UIImageView alloc] init];
        _avatarImageView.layer.cornerRadius = 15;
        _avatarImageView.layer.masksToBounds = YES;
        [_avatarImageView setImage:[UIImage imageNamed:@"default_avatar"]];
        self.contentView.addSubview(_date).addSubview(_from).addSubview(_to).addSubview(_nameLabel).addSubview(_phone).addSubview(_line).addSubview(_statusLabel).addSubview(_bottomLine).addSubview(_avatarImageView);
        // layout
        __weak typeof(self.contentView) weakSelf = self.contentView;
        [_date mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(weakSelf);
            make.height.mas_equalTo(kScaleHeight(44));
        }];
        [_from mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(20));
            make.top.equalTo(_date.mas_bottom).mas_offset(kScaleHeight(15));
            make.right.equalTo(_statusLabel.mas_left).mas_offset(-kScaleWidth(20));
        }];
        [_to mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_from.mas_left);
            make.bottom.equalTo(_line.mas_top).mas_offset(-kScaleHeight(12));
            make.top.equalTo(_from.mas_bottom).mas_offset(kScaleHeight(15));
            make.right.equalTo(_statusLabel.mas_left).mas_offset(-kScaleWidth(20));
        }];
        [_avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(20));
            make.width.height.mas_equalTo(@30);
            make.centerY.equalTo(_nameLabel.mas_centerY);
        }];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(kScaleHeight(40));
            make.right.equalTo(_phone.mas_left).mas_offset(-kScaleWidth(20));
            make.top.bottom.equalTo(_phone);
        }];
        [_phone mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(_nameLabel.mas_height);
            make.bottom.mas_equalTo(weakSelf.mas_bottom);
            make.top.equalTo(_line.mas_bottom);
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(14));
        }];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf);
            make.height.mas_equalTo(@1);
            make.top.equalTo(_to.mas_bottom).mas_offset(kScaleHeight(30));
        }];
        [_statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(20));
            make.top.equalTo(_date.mas_bottom);
            make.bottom.equalTo(_line.mas_top);
            make.width.mas_equalTo(@40);
        }];
        [_bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf);
            make.height.mas_equalTo(0.5);
            make.bottom.equalTo(weakSelf);
        }];
        _to.type = 1;
        _from.type = 2;
    }
    return self;
}

- (void)setModel:(HomeHistoryModel *)model {
    _model = model;
    _phone.text = model.phone;
    _nameLabel.text = model.passenger;
    _date.date = model.date;
    _from.address = model.destination;
    _to.address = model.origin;
    _statusLabel.text = model.complete;
    NSString *urlStr = [NSString stringWithFormat:@"%@/%@", kRootImageUrl, model.passengerHeadPic];
    [_avatarImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"default_avatar"]];
}

@end

@implementation HomeTableViewDate {
    UILabel * _dateLabel;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        self.backgroundColor = [UIColor colorWithString:@"#F0F8FF40"];
        _dateLabel = [[UILabel alloc] init];
        self.addSubview(_dateLabel);
        __weak typeof(self) weakSelf = self;
        [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(weakSelf);
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(25));
        }];
        _dateLabel.textColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setDate:(NSString *)date {
    _date = date;
    _dateLabel.text = date;
}

@end

@implementation HomeTableViewItem {
    UIImageView * _image;
    UILabel * _titleLabel;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        self.backgroundColor = [UIColor clearColor];
        _image = [[UIImageView alloc] initWithImage:nil];
        _image.contentMode = UIViewContentModeScaleToFill;
        _titleLabel = [[UILabel alloc] init];
        [_titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
        _titleLabel.numberOfLines = 0;
        _titleLabel.font = kAppFontSize(13);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        self.addSubview(_image).addSubview(_titleLabel);
        __weak typeof(self) weakSelf = self;
        [_image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left);
            make.centerY.equalTo(weakSelf);
            make.width.mas_equalTo(kScaleWidth(15));
            make.height.mas_equalTo(kScaleWidth(18));
        }];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.right.equalTo(weakSelf);
            make.left.equalTo(_image.mas_right).mas_offset(kScaleWidth(10));
        }];
    }
    return self;
}

- (void)setAddress:(NSString *)address {
    _address = address;
    _titleLabel.text = address;
}

- (void)setType:(NSInteger)type {
    _type = type;
    if(type != 1) {
        _image.image = [UIImage imageNamed:@"locationBegin"];
        _titleLabel.textColor = [UIColor colorWithString:@"#F09636"];
    } else {
        _image.image = [UIImage imageNamed:@"locationEnd"];
        _titleLabel.textColor = [UIColor whiteColor];
    }
}

@end
